/**
 * Copyright © 2020 易久批信息技术有限公司. All rights reserved.
 */
package com.dysm.prototype;

import com.alibaba.fastjson.JSON;

import java.io.*;
import java.util.Date;

/**
 * 原型模式
 * @author xiay
 * @since 2020/12/9 9:51 下午
 */
public class PrototypeClient {

    public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
//        shallowClone();
//        deepClone();
        serializableClone();
    }

    public static void shallowClone() throws CloneNotSupportedException {
        // 浅复制，克隆之后修改原对象的birthday值，克隆对象也会跟着变
        ShallowCloneUser shallowUser = new ShallowCloneUser();
        Date birthday1 = new Date();
        shallowUser.setBirthday(birthday1);
        shallowUser.setName("第一个浅复制用户");
        System.out.println(("shallowUser修改之前：" + JSON.toJSONString(shallowUser)));
        ShallowCloneUser cloneShallowCloneUser = (ShallowCloneUser) shallowUser.clone();
        System.out.println(("cloneShallowUser修改之前：" + JSON.toJSONString(cloneShallowCloneUser)));
        birthday1.setTime(3453464326345634L);
        System.out.println(("shallowUser修改之后：" + JSON.toJSONString(shallowUser)));
        System.out.println(("cloneShallowUser修改之后：" + JSON.toJSONString(cloneShallowCloneUser)));
    }

    public static void deepClone() throws CloneNotSupportedException {
        // 深复制，克隆之后修改原对象的birthday值，克隆对象不会跟着变
        DeepCloneUser deepUser = new DeepCloneUser();
        Date birthday2 = new Date();
        deepUser.setBirthday(birthday2);
        deepUser.setName("第一个深复制用户");
        System.out.println(("deepUser修改之前：" + JSON.toJSONString(deepUser)));
        DeepCloneUser cloneDeepCloneUser = (DeepCloneUser) deepUser.clone();
        System.out.println(("cloneDeepUser修改之前：" + JSON.toJSONString(cloneDeepCloneUser)));
        birthday2.setTime(3453454353L);
        System.out.println(("deepUser修改之后：" + JSON.toJSONString(deepUser)));
        System.out.println(("cloneDeepUser修改之后：" + JSON.toJSONString(cloneDeepCloneUser)));
    }

    public static  void serializableClone() throws IOException, ClassNotFoundException {
        // 序列化和反序列化
        SerializableUser serializableUser = new SerializableUser();
        Date birthday3 = new Date();
        serializableUser.setBirthday(birthday3);
        serializableUser.setName("第一个序列化用户");
        System.out.println(("serializableUser修改之前：" + JSON.toJSONString(serializableUser)));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(serializableUser);
        byte[] bytes = bos.toByteArray();
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bis);
        SerializableUser cloneSerializableUser = (SerializableUser)ois.readObject();
        System.out.println(("cloneSerializableUser修改之前：" + JSON.toJSONString(cloneSerializableUser)));
        birthday3.setTime(3453464326345634L);
        System.out.println(("serializableUser修改之后：" + JSON.toJSONString(serializableUser)));
        System.out.println(("cloneSerializableUser修改之后：" + JSON.toJSONString(cloneSerializableUser)));
    }
}
